package com.fr.base.core;

import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;

public class IteratorChain
  implements Iterator
{
  private Iterator[] iteratorChain = new Iterator[0];
  private int currentIteratorIndex = 0;
  private Iterator currentIterator = null;
  private Iterator lastUsedIterator = null;

  public static void main(String[] paramArrayOfString)
  {
    Object localObject;
    Object[] arrayOfObject1 = { "o11", "o12", "o13" };
    Object[] arrayOfObject2 = { "o21", "o22", "o23" };
    ArrayList localArrayList1 = new ArrayList();
    for (int i = 0; i < arrayOfObject1.length; ++i)
      localArrayList1.add(arrayOfObject1[i]);
    ArrayList localArrayList2 = new ArrayList();
    for (int j = 0; j < arrayOfObject2.length; ++j)
      localArrayList2.add(arrayOfObject2[j]);
    IteratorChain localIteratorChain = new IteratorChain(new Iterator[] { localArrayList1.iterator(), localArrayList2.iterator() });
    while (true)
    {
      do
      {
        if (!(localIteratorChain.hasNext()))
          break label185;
        localObject = localIteratorChain.next();
        System.out.println(localObject);
      }
      while (!(localObject.equals("o12")));
      localIteratorChain.remove();
    }
    label185: System.out.println("*******");
    localIteratorChain = new IteratorChain(new Iterator[] { localArrayList1.iterator(), localArrayList2.iterator() });
    while (true)
    {
      do
      {
        if (!(localIteratorChain.hasNext()))
          return;
        localObject = localIteratorChain.next();
        System.out.println(localObject);
      }
      while (!(localObject.equals("o12")));
      localIteratorChain.remove();
    }
  }

  public IteratorChain()
  {
  }

  public IteratorChain(Iterator[] paramArrayOfIterator)
  {
    if (paramArrayOfIterator != null)
    {
      this.iteratorChain = new Iterator[paramArrayOfIterator.length];
      System.arraycopy(paramArrayOfIterator, 0, this.iteratorChain, 0, paramArrayOfIterator.length);
    }
  }

  public int size()
  {
    return this.iteratorChain.length;
  }

  private void updateCurrentIterator()
  {
    if (this.currentIterator == null)
    {
      if (this.iteratorChain.length == 0)
      {
        this.currentIterator = EmptyIterator.access$000();
      }
      else
      {
        this.currentIterator = this.iteratorChain[this.currentIteratorIndex];
        this.currentIteratorIndex += 1;
      }
    }
    else if ((!(this.currentIterator.hasNext())) && (this.currentIteratorIndex < this.iteratorChain.length))
    {
      this.currentIterator = this.iteratorChain[this.currentIteratorIndex];
      this.currentIteratorIndex += 1;
    }
  }

  public boolean hasNext()
  {
    updateCurrentIterator();
    this.lastUsedIterator = this.currentIterator;
    return this.currentIterator.hasNext();
  }

  public Object next()
  {
    updateCurrentIterator();
    this.lastUsedIterator = this.currentIterator;
    return this.currentIterator.next();
  }

  public void remove()
  {
    if (this.currentIterator == null)
      updateCurrentIterator();
    this.lastUsedIterator.remove();
  }

  private static class EmptyIterator
    implements Iterator
  {
    private static final EmptyIterator INSTANCE = new EmptyIterator();

    public boolean hasNext()
    {
      return false;
    }

    public Object next()
    {
      throw new NoSuchElementException("Iterator contains no elements");
    }

    public void remove()
    {
      throw new IllegalStateException("Iterator contains no elements");
    }

    static EmptyIterator access$000()
    {
      return INSTANCE;
    }
  }
}